<?php
/**
 * @title 1620. 网络信号最好的坐标 - 暴力破解
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[][] $towers
     * @param Integer $radius
     * @return Integer[]
     */
    function bestCoordinate($towers, $radius) {
        /**
         * @since 2020-10-19 半径内, 整数点
         */
        $points = [];
        for($x = 0-$radius; $x<=$radius; $x++){
            for($y = 0-$radius; $y<=$radius; $y++){
                $d = sqrt($x*$x+$y*$y);
                if($d <= $radius){
                    $points[] = [$x, $y];
                } else {}
            }
        }

        /**
         * @since 2020-10-19 全局变量
         */
        $this->towers = $towers;
        $this->radius = $radius;

        /**
         * @since 2020-10-19 缓存
         */
        $dps = [];

        /**
         * @since 2020-10-19 最大信号
         */
        $max = 0;
        $maxX = 0;
        $maxY = 0;

        /**
         * @since 2020-10-19 穷列每个基站
         */
        foreach ($towers as list($x, $y, $q)){
            foreach ($points as list($addX, $addY)){
                $x1 = $x+$addX;
                $y1 = $y+$addY;

                /**
                 * @since 2020-10-19 防止重复计算
                 */
                if(!isset($dps[$x1][$y1])){
                    $sum = self::find($x1, $y1);
                    $dps[$x1][$y1] = $sum;

                    if($sum > $max){
                        $max = $sum;
                        $maxX = $x1;
                        $maxY = $y1;
                    } elseif($sum == $max) {
                        if($x1 < $maxX){
                            $maxX = $x1;
                            $maxY = $y1;
                        } elseif($x == $maxX AND $y1<$maxY) {
                            $maxX = $x1;
                            $maxY = $y1;
                        }
                    }
                } else {}
            }
        }

        /**
         * @return
         */
        // return $dps;
        // return [$max, $maxX, $maxY];
        return [$maxX, $maxY];
    }

    /**
     * @title 求基站信号之和
     * @author start2004
     * @since 2020-10-19 Created
     *
     * @param int $x1
     * @param int $y1
     */
    public function find($x1=0, $y1=0){
        $sum = 0;

        /**
         * @since 2020-10-19 逐个基站求信号
         */
        foreach ($this->towers as list($x, $y, $q)){
            $d = sqrt(pow($x1-$x, 2) + pow($y1-$y, 2));
            if($d <= $this->radius){
                $s = intval($q/(1+$d));
                // echo "({$x1}, {$y1}) - ({$x}, {$y}), d={$d}, s={$s}", PHP_EOL;
                $sum += $s;
            } else {}
        }

        /**
         * @return
         */
        return $sum;
    }
}

/**
 * @url http://127.0.0.1/leetcode/202010/2020.10.17.php
 */
$datas = [
    [[[1,2,5],[2,1,7],[3,1,9]], 2], // [2,1]
    [[[23,11,21]], 9], // [23,11]
    [[[1,2,13],[2,1,7],[0,1,9]], 2], // [1,2]
    [[[2,1,9],[0,1,9]], 2], // [0,1]
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    // var_dump($data);

    $obj = new Solution();
    $result = $obj->bestCoordinate(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $result = $obj->$data[0][$i](...$data[1][$i]);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
